// 个人信息展示组件
Vue.component('profile-info', {
  props: ['info'],
  data() {
    return {
      dialogVisible: false,
      editInfo: { ...this.info }
    };
  },
  watch: {
    info: {
      handler(val) {
        this.editInfo = { ...val };
        // 处理出生日期格式
        if (this.editInfo.birth_date === '未设置' || !this.editInfo.birth_date) {
          this.editInfo.birth_date = null;
        }
      },
      deep: true
    }
  },
  template: `
    <div>
      <div style="font-size: 22px; font-weight: bold; margin-bottom: 18px;">个人信息</div>
      <el-descriptions border column="1">
        <el-descriptions-item label="姓名">{{info.name}}</el-descriptions-item>
        <el-descriptions-item label="性别">{{info.gender}}</el-descriptions-item>
        <el-descriptions-item label="出生日期">{{info.birth_date}}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{info.phone}}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{info.email}}</el-descriptions-item>
        <el-descriptions-item label="部门">{{info.department_name}}</el-descriptions-item>
        <el-descriptions-item label="职位">{{info.position}}</el-descriptions-item>
        <el-descriptions-item label="地址">{{info.address}}</el-descriptions-item>
      </el-descriptions>
      <div style="text-align:right;margin-top:20px;">
        <el-button type="primary" size="mini" @click="dialogVisible=true">修改信息</el-button>
      </div>
      <el-dialog title="修改个人信息" :visible.sync="dialogVisible" width="600px">
        <el-form label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="editInfo.name"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="editInfo.gender" placeholder="请选择">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="出生日期">
            <el-date-picker 
              v-model="editInfo.birth_date" 
              type="date" 
              placeholder="选择日期" 
              style="width:100%"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              clearable>
            </el-date-picker>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="editInfo.phone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="editInfo.email"></el-input>
          </el-form-item>
          <el-form-item label="部门">
            <el-input v-model="editInfo.department_name" disabled></el-input>
          </el-form-item>
          <el-form-item label="职位">
            <el-input v-model="editInfo.position"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="editInfo.address"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible=false">取消</el-button>
          <el-button type="primary" @click="saveInfo">保存</el-button>
        </span>
      </el-dialog>
    </div>
  `,
  methods: {
    async saveInfo() {
      try {
        // 准备要发送的数据，排除部门名称（只读字段）
        const updateData = { ...this.editInfo };
        delete updateData.department_name; // 移除部门名称，因为这是只读字段
        
        await authAPI.updateProfile(updateData);
        this.$message.success('个人信息修改成功');
        this.dialogVisible = false;
        Object.assign(this.info, this.editInfo);
        
        // 更新localStorage中的用户信息
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
        Object.assign(userInfo, this.editInfo);
        localStorage.setItem('userInfo', JSON.stringify(userInfo));
        
        // 触发全局事件，通知其他组件更新
        this.$root.$emit('user-info-updated', userInfo);
      } catch (error) {
        console.error('修改个人信息失败:', error);
        this.$message.error(error.message || '修改失败');
      }
    }
  },
  mounted() {
    // 初始化时处理出生日期格式
    if (this.editInfo.birth_date === '未设置' || !this.editInfo.birth_date) {
      this.editInfo.birth_date = null;
    }
  }
});